/**
 * Exam No: Y8467136
 */
package japrc2011test;

import static org.junit.Assert.*;
import japrc2011.GridLocation;
import japrc2011.MapEntity;
import japrc2011.MapTableModel;
import japrc2011.Robot;
import japrc2011.RobotSimulator;
import japrc2011.SimulatorInterface;

import org.junit.Before;
import org.junit.Test;

public class SimulationLevel2Test {
    

    MapTableModel aMap;
	
    private SimulatorInterface rs;
    //Robot r;
    
    @Before
    public void setUp() throws Exception
    {
        rs = new RobotSimulator();
    }
    
	
	/**
	 * Test if the wall is added and also if the wall is impassable to robots.
	 */
	@Test
	public void testAddWall_ImpassableWall() {
		MapEntity[][] current_map2;
		rs.addRobot(new Robot("r1"));
		assertEquals(new GridLocation(0,0), rs.getRobot("r1").getLocation());
		rs.addRobot(new Robot("r2"));
		assertEquals(new GridLocation(0,0), rs.getRobot("r2").getLocation());
		rs.addWall(new GridLocation(2,1));
		current_map2 = rs.getMapState();
		assertEquals("WALL: wall", (current_map2[2][1]).toString());
		
		rs.sendCommand("r2", "M 2 1");
        rs.tick();
		assertEquals(new GridLocation(1,1), rs.getRobot("r2").getLocation());
        rs.sendCommand("r1", "M 2 0");//go east
        rs.tick();
		assertEquals(new GridLocation(2,0), rs.getRobot("r1").getLocation());
        rs.sendCommand("r1", "M 0 1");//go south
        rs.tick();
		assertEquals(new GridLocation(2,0), rs.getRobot("r1").getLocation());          
	}
	
	/**
	 * Test if robots can move through each other
	 */
	@Test
	public void testIfRobot1MoveThroughRobot2() {
		rs.addRobot(new Robot("r1"));
        assertEquals(1, rs.getRobots().size()); 
        rs.addRobot(new Robot("r2"));
        assertEquals(2, rs.getRobots().size());
        rs.sendCommand("r2", "M 1 1");
        rs.tick();
		assertEquals(new GridLocation(1,1), rs.getRobot("r2").getLocation());
		//check using multi-square command. The robot remains in the same place as it can not move through the other robot.
		rs.sendCommand("r1", "M 2 2");
        rs.tick();
		assertEquals(new GridLocation(0,0), rs.getRobot("r1").getLocation());
		//check using a set of simple commands.
		rs.sendCommand("r1", "M  1 0");//move east. The cell is free so the robot can go there.
        rs.tick();
		assertEquals(new GridLocation(1,0), rs.getRobot("r1").getLocation());
		rs.sendCommand("r1", "M 0 1");//move south. In the gridLocation(1,1) is the r2 robot. So r1 robot can not move there.
        rs.tick();
		assertEquals(new GridLocation(1,0), rs.getRobot("r1").getLocation());
	}
	
	/**
	 * Change the size of the map in any dimension
	 */
	@Test
	public void testMapSize() { 
		aMap = new MapTableModel();
		rs.setMapSize(4, 4);
		int aX = aMap.getRowCount();
		int aY = aMap.getColumnCount();
		aMap = new MapTableModel(); 
		assertEquals(4, aX);
		assertEquals(4, aY);
		
		rs.setMapSize(5, 8);
		aX = aMap.getRowCount();
		aY = aMap.getColumnCount();
		aMap = new MapTableModel(); 
		assertEquals(5, aX);
		assertEquals(8, aY);
	}
	
}
